package com.ginmeta.common.base;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.util.HashMap;

/**
 * 统一响应
 * @author GinMeta
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AjaxResult extends HashMap<String, Object> {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private static final String CODE = "code";

    /**
     * 消息
     */
    private static final String MSG = "msg";

    /**
     * 数据
     */
    private static final String DATA = "data";

    public AjaxResult(int code, String msg, Object data) {
        super.put(CODE, code);
        super.put(MSG, msg);
        if (ObjectUtil.isNotNull(data)) {
            super.put(DATA, data);
        }
    }

    public static AjaxResult success() {
        return AjaxResult.success("操作成功");
    }

    public static AjaxResult success(Object data) {
        return AjaxResult.success("操作成功", data);
    }

    public static AjaxResult success(String msg) {
        return AjaxResult.success(msg,null);
    }

    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(HttpStatus.HTTP_OK, msg, data);
    }
    public static AjaxResult success(int code, String msg) {
        return new AjaxResult(code, msg, null);
    }


    public static AjaxResult error() {
        return AjaxResult.error("操作失败");
    }

    public static AjaxResult error(Object data) {
        return AjaxResult.error("操作失败", data);
    }

    public static AjaxResult error(String msg) {
        return AjaxResult.error(msg,null);
    }

    public static AjaxResult error(String msg, Object data) {
        return new AjaxResult(HttpStatus.HTTP_INTERNAL_ERROR, msg, data);
    }

    public static AjaxResult error(int code) {
        return AjaxResult.error(code, "操作失败");
    }

    public static AjaxResult error(int code, Object data) {
        return AjaxResult.error(code, "操作失败", data);
    }

    public static AjaxResult error(int code, String msg) {
        return AjaxResult.error(code, msg, null);
    }

    public static AjaxResult error(int code, String msg, Object data) {
        return new AjaxResult(code, msg, data);
    }
}
